package com.caocaog.market.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.caocaog.market.entity.ShopPromo;
import com.caocaog.market.entity.ShopPromoHis;
import com.caocaog.market.service.ShopPromoHisService;
import com.caocaog.market.service.ShopPromoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.aspect.annotation.Trim;
import org.jeecg.common.system.base.controller.GenericController;
import org.jeecg.common.system.domain.QueryRequest;
import org.jeecg.common.system.query.PageGenerator;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

/**
 * 商品促销信息
 *
 * @author chechen
 * @date 2021-11-20
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@Api(value = "shopPromo", tags = ShopPromoController.TABLE_MEANING)
@RequestMapping("/shopPromo")
public class ShopPromoController extends GenericController<ShopPromo, ShopPromoService> {

	private final ShopPromoHisService shopPromoHisService;
	public static final String TABLE_MEANING = "商品促销信息";

	private static final String PAGE = "商品促销信息-分页列表查询";
	@Trim
	@ApiOperation(value = PAGE, notes = PAGE)
	@GetMapping(value = "/list")
	@RequiresPermissions("shopPromo:view")
	public Result<?> queryShopPromoPage(ShopPromo shopPromo, QueryRequest queryRequest) {
		Page<ShopPromo> page = PageGenerator.createPageSort(queryRequest);
		return Result.OK(super.service.queryShopPromoPage(page, shopPromo));
	}

	private static final String HIS_PAGE = "商品促销历史信息-分页列表查询";
	@ApiOperation(value = HIS_PAGE, notes = HIS_PAGE)
	@GetMapping(value = "/hisList")
	@RequiresPermissions("shopPromo:view")
	public Result<?> queryShopPromoHisPage(ShopPromoHis shopPromoHis, QueryRequest queryRequest) {
		Page<ShopPromoHis> page = PageGenerator.createPageSort(queryRequest);
		return Result.OK(this.shopPromoHisService.queryShopPromoHisPage(page, shopPromoHis));
	}

	private static final String ADD = "商品促销信息-添加";
	@AutoLog(value = ADD)
	@ApiOperation(value = ADD, notes = ADD)
	@PostMapping(value = "/add")
	@RequiresPermissions("shopPromo:add")
	public Result<?> add(@RequestBody ShopPromo shopPromo) {
		return Result.OK(ADD_SUCCESS, super.service.addShopPromo(shopPromo));
	}

	private static final String EDIT = "商品促销信息-编辑";
	@AutoLog(value = EDIT)
	@ApiOperation(value = EDIT, notes = EDIT)
	@PutMapping(value = "/edit")
	@RequiresPermissions("shopPromo:edit")
	public Result<?> edit(@RequestBody ShopPromo shopPromo) {
		return Result.OK(EDIT_SUCCESS, super.service.updateShopPromo(shopPromo));
	}

	private static final String DELETE = "商品促销信息-通过id删除";
	@AutoLog(value = DELETE)
	@ApiOperation(value = DELETE, notes = DELETE)
	@DeleteMapping(value = "/delete")
	@RequiresPermissions("shopPromo:delete")
	public Result<?> delete(@RequestParam(name = "id") String id) {
		return Result.OK(DELETE_SUCCESS, super.service.deleteShopPromo(id));
	}

	private static final String DELETE_BATCH = "商品促销信息-批量删除";
	@AutoLog(value = DELETE_BATCH)
	@ApiOperation(value = DELETE_BATCH, notes = DELETE_BATCH)
	@DeleteMapping(value = "/deleteBatch")
	@RequiresPermissions("shopPromo:delete")
	public Result<?> deleteBatch(@RequestParam(name = "ids") String ids) {
		return Result.OK(DELETE_SUCCESS, super.service.deleteBatchShopPromo(Arrays.asList(ids.split(","))));
	}

	private static final String STATUS_CHANGE = "商品促销信息-状态变更";
	@AutoLog(value = STATUS_CHANGE)
	@ApiOperation(value = STATUS_CHANGE, notes = STATUS_CHANGE)
	@PutMapping(value = "/statusChange")
	@RequiresPermissions("shopPromo:status")
	public Result<?> statusChange(@RequestParam(name = "id") String id) {
		return Result.OK("状态变更成功", super.service.shopPromoStatusChange(id));
	}

}
